package com.somnus.demo.androiddatabingdemo;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;

import com.somnus.demo.androiddatabingdemo.adapter.RecycerViewAdapter;
import com.somnus.demo.androiddatabingdemo.bean.MovieBean;
import com.somnus.demo.androiddatabingdemo.databinding.ActivityMovieBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Somnus on 2017/3/14.
 *
 * @Description:
 */

public class MovieActivity extends AppCompatActivity {

    private RecycerViewAdapter mRecycerViewAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ActivityMovieBinding movieBinding = DataBindingUtil.setContentView(this, R.layout.activity_movie);
        MovieBean movieBean = new MovieBean();
        movieBean.setAuthor("禮拜");

        movieBinding.setVariable(BR.movie, movieBean);


        mRecycerViewAdapter = new RecycerViewAdapter(this, genData());
        movieBinding.recyclerViewMovie.setLayoutManager(new LinearLayoutManager(this));
        movieBinding.recyclerViewMovie.setAdapter(mRecycerViewAdapter);
        movieBinding.recyclerViewMovie.setItemAnimator(new DefaultItemAnimator());


    }

    private List<MovieBean> genData() {
        ArrayList<MovieBean> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            String name = "Somnus";
            int len = random.nextInt(80) + 60;
            MovieBean movie = new MovieBean();
            movie.setAuthor(i + "禮拜" + name);
            movie.setCreateTime("2017年3月14日11:24:49==" + i);
            movie.setTitle("i" + i + "古樹");
            movie.setLength(len);
            list.add(movie);
        }
        return list;
    }
}
